require 'zdc.doc'

module(..., package.seeall)

local AppMag = {}
local AppMag_mt = {__metatable = {}, __index = AppMag}

function new(params)
	local obj = setmetatable({_map={}},AppMag_mt)
	return obj
end

function AppMag:AddOne(appid,app)
	local slot=self._map[appid]
	if slot then
		slot[app]=app
	else
		self._map[appid]={[app]=app}
	end
end

function AppMag:DelOne(appid,app)
	local slot=self._map[appid]
	if slot then
		slot[app]=nil
	end
end

function AppMag:FindApp(appid,cli)
	local slot=self._map[appid]
	local ap
	if slot then
		local a,ca=100000000
		for k,v in pairs(slot) do
			ca=k:GetBusy()
			if ca<a then
				ap=k
				a=ca
				if a==0 then break end
			end
		end
	end
	return ap
end
